<?php

\think\Route::pattern([
    'id'   =>  '\d+',
    'mid'  =>  '\d+',
]);
\think\Route::register([

    //前台路由定义
    'search/:mid'  =>  'index/index/search',
    't/:flag'  =>  'index/index/term',
    'c/:id'  =>  'index/index/content',
    //'msg/:id/:term_id'  =>  'index/index/message',
]);

if(APP_DEBUG){
    \think\Route::register([

        //开发工具路由定义
        '[devel]'   =>  [
            'search/:mid'  =>  'devel/test/search',
            't/:flag'  =>  'devel/test/term',
            'c/:id'  =>  'devel/test/content',
            //'msg/:id/:term_id'  =>  'index/index/message',
        ],
        'devel$'   => 'devel/test/index',
    ]);
}

if(!APP_DEBUG) \think\Config::set('log.type', 'test');

/**
 *  RC2没有msubstr,从tp3.2搬过来了
 */
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) {
    if(function_exists("mb_substr")){
        $slice = mb_substr($str, $start, $length, $charset);
    	$str_length = mb_strlen($str, $charset);
    }elseif(function_exists('iconv_substr')) {
        $slice = iconv_substr($str,$start,$length,$charset);
    }else{
        $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("",array_slice($match[0], $start, $length));
    }
    if($suffix){
    	if($str_length > $length ){
    		$slice .= "...";
    	}
    }
    return $slice;
}


/**
 * 获取客户端IP地址
 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
 * @return mixed
 */
function get_client_ip($type = 0) {
	$type       =  $type ? 1 : 0;
    static $ip  =   NULL;
    if ($ip !== NULL) return $ip[$type];
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $pos    =   array_search('unknown',$arr);
        if(false !== $pos) unset($arr[$pos]);
        $ip     =   trim($arr[0]);
    }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ip     =   $_SERVER['HTTP_CLIENT_IP'];
    }elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip     =   $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u",ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}


/**
 * 获取当前页面完整URL地址
 */
    function get_url(){
    $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
    $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
    $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
    $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
    return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
    }
	
	
	/**
	 * 验证码验证
	 */
	function check_verify($code, $id =''){
			$Verify = new \org\Verify();
			return $Verify->check($code, $id);
	} 

	
	/**
	 *  关于数据库备份
	 */

		function rmdirr($dirname) {
			if (!file_exists($dirname)) {
				return false;
			}
			if (is_file($dirname) || is_link($dirname)) {
				return unlink($dirname);
			}
			$dir = dir($dirname);
			if ($dir) {
				while (false !== $entry = $dir->read()) {
					if ($entry == '.' || $entry == '..') {
						continue;
					}
					//递归
					rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
				}
			}
		}

    //获取文件修改时间
		function getfiletime($file, $DataDir) {
			$a = filemtime($DataDir . $file);
			$time = date("Y-m-d H:i:s", $a);
			return $time;
		}

   //获取文件的大小
		function getfilesize($file, $DataDir) {
			$perms = stat($DataDir . $file);
			$size = $perms['size'];
			// 单位自动转换函数
			$kb = 1024;         // Kilobyte
			$mb = 1024 * $kb;   // Megabyte
			$gb = 1024 * $mb;   // Gigabyte
			$tb = 1024 * $gb;   // Terabyte

			if ($size < $kb) {
				return $size . " B";
			} else if ($size < $mb) {
				return round($size / $kb, 2) . " KB";
			} else if ($size < $gb) {
				return round($size / $mb, 2) . " MB";
			} else if ($size < $tb) {
				return round($size / $gb, 2) . " GB";
			} else {
				return round($size / $tb, 2) . " TB";
			}
		}
		

	/**
	 *  获取访客来源
	 *  return 搜索引擎来路返回其名字，否则为完整域名
	 */
	
			function get_referer()
			{  
			   //暂时写这么多,不打算写到数据库,就那么几家出名的
			    $SE=array("baidu"=>"百度","sogou"=>"搜狗","so"=>"360搜索","google"=>"谷歌","bing"=>"必应");
				$url_first = isset($_SERVER["HTTP_REFERER"])?$_SERVER["HTTP_REFERER"]:get_url(); 
                $url = strtolower($url_first);		   
				$if_http = stripos($url,"://");  
				$if_search= stripos($url,"link?url=");
				
				if(!isset($_SERVER["HTTP_REFERER"])){
					$strdomain[0] ="直接访问";
					return $strdomain[0];
				}
				
				if($if_http){
					  $str_http = str_replace("http://","",$url);			  
					  $strdomain = explode("/",$str_http);
						  if($if_search){
							 $strdomain = explode(".",$str_http);
							foreach($SE as $key=>$value){
								if(in_array("$key",$strdomain)){
								$strdomain[0] ="$value";
								return $strdomain[0];
							  } 
							 }	 
						  }else{
							 return $strdomain[0];
						  }				  
				}elseif(!$if_http){
						  $str_http = str_replace("https://","",$url);			  
						  $strdomain = explode("/",$str_http);
							  if($if_search){
								 $strdomain = explode(".",$str_http);
							foreach($SE as $key=>$value){
								if(in_array("$key",$strdomain)){
								$strdomain[0] ="$value";
								return $strdomain[0];
							  } 
							}		 
						  }else{
							 return $strdomain[0];
						  }
						  
				}
				//应该包含付费推广等链接，暂时不做
	
			}  
			
		/**
		 *  生成随机颜色
		 */
           
		
		function randrgb()  
		{  
		  $str='0123456789ABCDEF';  
			$estr='#';  
			$len=strlen($str);  
			for($i=1;$i<=6;$i++)  
			{  
				$num=rand(0,$len-1);    
				$estr=$estr.$str[$num];   
			}  
			return $estr;  
		}
